﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using MapEntities;
using System.Collections.Generic;
using System.Resources;
using System.IO;
using System.Windows.Media.Imaging;

namespace VirtualEarth.Business
{
    public class GeoSymbol: GeoItem
    {
        public GeoSymbol(Map map, MapEntities.Symbol symbol)
            : base(map, symbol)
        {
        }

        private Image GetImage()
        {  
            Uri xuri = new Uri( "Resources/" + Item.SymbolTypeName.ToString() + ".png",UriKind.Relative);         
            BitmapImage xbmp = new BitmapImage(xuri);
            Image xsymbol = new Image();
            xsymbol.Source = xbmp;
            xsymbol.Width = 32;
            xsymbol.Height = 32;
            Canvas.SetZIndex(xsymbol, 1);
            xsymbol.MouseEnter += new MouseEventHandler(xsymbol_MouseEnter);
            xsymbol.MouseLeave += new MouseEventHandler(xsymbol_MouseLeave);
            xsymbol.MouseLeftButtonUp += new MouseButtonEventHandler(xsymbol_MouseLeftButtonUp);
            xsymbol.SetValue(MapLayer.PositionProperty, new Microsoft.Maps.MapControl.Location(Item.Location.Lat, (Item.Location.Long)));
            xsymbol.SetValue(MapLayer.PositionOriginProperty, PositionOrigin.Center);      
            if (!string.IsNullOrEmpty(Item.Tooltip))
                ToolTipService.SetToolTip(xsymbol, Item.Tooltip);        
            return xsymbol;
        }

        void xsymbol_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image p = (Image)e.OriginalSource;
            GeoSymbol shape = (GeoSymbol)(p.GetValue(MapLayer.DataContextProperty));
            DoBehaviour(shape.Item.Behaviour.LeftMouseClick);
        }

        public override void Visualize()
        {            
            if(Item.Visible && Item.SymbolTypeName != SymbolType.None) AddToBaseControl(GetImage());
        }

        void xsymbol_MouseLeave(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            Canvas.SetZIndex(i, Canvas.GetZIndex(i) - 2);           
            i.RenderTransform = null;

        }

        void xsymbol_MouseEnter(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 2.0;
            st.ScaleY = 2.0;
            st.CenterX = i.Height / 2;
            st.CenterY = i.Height / 2;
            Canvas.SetZIndex(i, Canvas.GetZIndex(i) + 2);           
            i.RenderTransform = st;
        }

        public new Symbol Item
        {
            get
            {
                return (Symbol)(base.Item);
            }
        }
    }
}
